package com.dkd.manage.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.dkd.common.utils.DateUtils;
import com.dkd.manage.domain.dto.ChannelConfigDto;
import com.dkd.manage.domain.vo.ChannelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.ChannelMapper;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.service.IChannelService;

/**
 * 售货机货道Service业务层处理
 * 
 * @author hm
 * @date 2024-11-11
 */
@Service
public class ChannelServiceImpl implements IChannelService 
{
    @Autowired
    private ChannelMapper channelMapper;

    /**
     * 查询售货机货道
     * 
     * @param id 售货机货道主键
     * @return 售货机货道
     */
    @Override
    public Channel selectChannelById(Long id)
    {
        return channelMapper.selectChannelById(id);
    }

    /**
     * 查询售货机货道列表
     * 
     * @param channel 售货机货道
     * @return 售货机货道
     */
    @Override
    public List<Channel> selectChannelList(Channel channel)
    {
        return channelMapper.selectChannelList(channel);
    }

    /**
     * 新增售货机货道
     * 
     * @param channel 售货机货道
     * @return 结果
     */
    @Override
    public int insertChannel(Channel channel)
    {
        channel.setCreateTime(DateUtils.getNowDate());
        return channelMapper.insertChannel(channel);
    }

    /**
     * 修改售货机货道
     * 
     * @param channel 售货机货道
     * @return 结果
     */
    @Override
    public int updateChannel(Channel channel)
    {
        channel.setUpdateTime(DateUtils.getNowDate());
        return channelMapper.updateChannel(channel);
    }

    /**
     * 批量删除售货机货道
     * 
     * @param ids 需要删除的售货机货道主键
     * @return 结果
     */
    @Override
    public int deleteChannelByIds(Long[] ids)
    {
        return channelMapper.deleteChannelByIds(ids);
    }

    /**
     * 删除售货机货道信息
     * 
     * @param id 售货机货道主键
     * @return 结果
     */
    @Override
    public int deleteChannelById(Long id)
    {
        return channelMapper.deleteChannelById(id);
    }

    @Override
    public int countChannelBySkuIds(Long[] skuIds) {
        int count = 0;
        for (Long skuId : skuIds) {
            Channel channel = new Channel();
            channel.setSkuId(skuId);
            List<Channel> channelList = channelMapper.selectChannelList(channel);
            count += channelList.size();
            System.out.println(channelList.size());
        }
        return count;
    }

    @Override
    public List<ChannelVo> selectChannelVoByInnerCode(String innerCode) {
        return channelMapper.selectChannelVoByInnerCode(innerCode);
    }

    /**
     * 设置售货机货道
     * @param channelConfigDto
     * @return
     */
    @Override
    public int setChannelConfig(ChannelConfigDto channelConfigDto) {

        System.out.println("-------------------");
        System.out.println("channelConfigDto:"+channelConfigDto.getChannelList().size());
        // 1、将dto转为po对象
        List<Channel> channelList = channelConfigDto.getChannelList().stream().map(channelSkuDto -> {
            Channel channel = channelMapper.selectChannelByInnerCodeAndChannelCode(channelSkuDto.getInnerCode(), channelSkuDto.getChannelCode());
            if (channel != null) {
                channel.setSkuId(channelSkuDto.getSkuId());
                channel.setUpdateTime(DateUtils.getNowDate());
            }
            return channel;
        }).collect(Collectors.toList());
        // 2、完成修改
        return channelMapper.batchUpdateChannel(channelList);
    }
}
